from enum import auto
import dash
from dash import dcc, html, dash_table
import dash_bootstrap_components as dbc
# from .modals import  create_new_repair_modal ,create_repair_complete_modal
from modules.repair.modals import create_repair_respond_modal,create_upload_proof_modal,create_new_repair_modal ,create_repair_complete_modal
from models import Product,RepairRecord

# 更新维修状态选项
status_options = [
    {'label': '待响应', 'value': '待响应'},
    {'label': '维修中', 'value': '维修中'},
    {'label': '已完成', 'value': '已完成'},
    
]




def create_repair_layout():
    layout = html.Div([
        dcc.Store(id='repair-details-record-id-store'),
        dcc.Store(id='repair-refresh-flag-addnew'),
        dcc.Store(id='repair-refresh-flag-answer'),
        dcc.Store(id='repair-refresh-flag-complete'),
        dcc.Store(id='repair-refresh-flag-cancel'),
        html.Link(rel='stylesheet', href='/static/css/equipment.css'), # 引入equipment.css
        # 查询条件区域（同一行）
        html.Div([
            html.Div([
                dcc.Dropdown(id='repair-org-filter', options=[], placeholder='选择机构', multi=False, searchable=True, style={"minWidth": "120px", "height": "38px", "marginRight": "8px"}),
                dcc.Dropdown(id='repair-dept-filter', options=[], placeholder='选择科室', multi=False, searchable=True, style={"minWidth": "110px", "height": "38px", "marginRight": "8px"}),
                dcc.Input(id='repair-id-filter', placeholder='设备ID', type='text', style={"width": "90px", "height": "38px", "marginRight": "8px"}),
                dcc.Input(id='repair-keyword-filter', placeholder='产品名/编号', type='text', style={"width": "110px", "height": "38px", "marginRight": "8px"}),
                dcc.Dropdown(id='repair-status-filter', options=status_options, placeholder='完成状态', multi=False, searchable=False, style={"minWidth": "100px", "height": "38px", "marginRight": "8px"}),
                dcc.DatePickerRange(
                    id='repair-date-filter',
                    display_format='YYYY-MM-DD',
                    start_date_placeholder_text='报修开始日期',
                    end_date_placeholder_text='报修结束日期',
                    style={"height": "38px", "marginRight": "8px", "cursor": "pointer"}
                ),
                dcc.Dropdown(id='repair-person-filter', options=[], placeholder='维修人员', multi=True, searchable=True, style={"minWidth": "110px", "height": "38px", "marginRight": "8px"}),
                dcc.Dropdown(id='repair-caller-filter', options=[], placeholder='报修人', multi=True, searchable=True, style={"minWidth": "110px", "height": "38px", "marginRight": "8px"}),
                html.Button("筛选", id="repair-filter-button", className="btn btn-primary me-2", style={"height": "38px", "marginRight": "8px", "boxShadow": "1px 1px 3px #bbb", "cursor": "pointer"}),
                html.Button("重置", id="repair-reset-button", className="btn btn-secondary", style={"height": "38px", "marginRight": "8px", "boxShadow": "1px 1px 3px #bbb", "cursor": "pointer"}),
                html.Button("导出列表", id="repair-export-btn", n_clicks=0, style={"height": "38px", "marginRight": "8px", "background": "#5cb85c", "color": "#fff", "border": "none", "boxShadow": "1px 1px 3px #bbb", "cursor": "pointer"}),
                
               
            ], style={
                "display": "flex",
                "flexWrap": "wrap",
                "alignItems": "center",
                "gap": "0.5rem"
            })
        ],   style={"width": "100%", "margin": "0 auto", "padding": "0 5px"}),

        # 维修记录表格区域
        html.Div([
            dash_table.DataTable(
                id='repair-records-table',
                columns=[
                    {'name': '报修单ID', 'id': 'id', 'type': 'numeric'},
                    {'name': '设备ID', 'id': 'product_id', 'type': 'numeric'},
                    {'name': '设备名称', 'id': 'product_name', 'type': 'text'},
                    {'name': '所属机构', 'id': 'org_shortname', 'type': 'text'},
                    {'name': '所属科室', 'id': 'department_name', 'type': 'text'},
                    {'name': '故障描述', 'id': 'fault_description', 'type': 'text'},
                    {'name': '报修时间', 'id': 'report_date', 'type': 'datetime'},
                    {'name': '完成状态', 'id': 'repair_status', 'type': 'text'},
                    {'name': '完成时间', 'id': 'actual_completion_date', 'type': 'datetime'},
                    {'name': '维修时长', 'id': 'repair_duration', 'type': 'numeric'},
                    {'name': '维修师', 'id': 'repair_person', 'type': 'text'},
                    {'name': '维修公司', 'id': 'repair_company', 'type': 'text'},
                    {'name': '维修类型', 'id': 'repair_type', 'type': 'text'},
                    {'name': '维修内容', 'id': 'repair_content', 'type': 'text'},
                    {'name': '维修费用', 'id': 'repair_cost', 'type': 'text'},
                    {'name': '维修配件', 'id': 'parts_replaced', 'type': 'text'},
                    {'name': '维修结果', 'id': 'repair_result', 'type': 'text'},
                    {'name': '备注', 'id': 'remarks', 'type': 'text'},
                ],
                data=[],
                page_size=2000,
                page_current=0,
                page_action='native',
                sort_action='native',
                sort_mode='multi',
                # row_selectable='single',
                selected_rows=[],
                style_table={'overflowX': 'auto', 'height': 'calc(100vh - 320px)', 'overflowY': 'auto'},
                style_cell={
                    'textAlign': 'left',
                    'minWidth': '80px', 'width': '150px', 'maxWidth': '200px',
                    'overflow': 'hidden',
                    'textOverflow': 'ellipsis',
                    'borderRight': '1px solid #dee2e6'
                },
                style_cell_conditional=[
                    {
                        'if': {'column_id': 'repair_status'},
                        'borderRight': 'none'
                    }
                ],
                style_header={
                    'backgroundColor': '#1a3a5f',
                    'color': '#ffffff',
                    'fontWeight': 'bold',
                    'borderRight': '1px solid #dee2e6'
                },
                style_header_conditional=[
                    {
                        'if': {'column_id': 'repair_status'},
                        'borderRight': 'none'
                    }
                ],
                style_data_conditional=[
                    {
                        'if': {'row_index': 'odd'},
                        'backgroundColor': '#234a78'
                    },
                    {
                        'if': {'row_index': 'even'},
                        'backgroundColor': '#1a3a5f'
                    },
                    {
                        'if': {'state': 'selected'},
                        'backgroundColor': '#e6f7ff',
                        'border': '1px solid #91d5ff',
                        'color': '#333'
                    }
                ]
            ),
            html.Div(id='repair-pagination-info', className="pagination-info mt-2"),
            dcc.Store(id='repair-selected-record-id'),
            html.Div(id='repair-action-feedback'),
            dcc.Upload(
                id='upload-work-order',
                children=html.Div(['拖拽或 ', html.A('选择电子工单文件')]),
                style={'width': '100%', 'height': '60px', 'lineHeight': '60px', 'borderWidth': '1px', 'borderStyle': 'dashed', 'borderRadius': '5px', 'textAlign': 'center', 'margin': '10px 0', 'display': 'none'},
                multiple=False
            ),
            html.Div(id='upload-output')
        ], className="list-container", style={"width": "100%", "border": "1px dashed #ccc", "padding": "10px"}),

        # 保留所有Modal弹窗
        create_repair_respond_modal(),
        create_new_repair_modal(),
        create_upload_proof_modal(),
        create_repair_complete_modal(),
        dbc.Modal(
            [
                dbc.ModalHeader("用户确认操作"),
                dbc.ModalBody("确认取消维修订单。"),
                dbc.ModalFooter([
                    dbc.Button("否", id="repair-cancel-no", color="secondary", className="me-2"),
                    dbc.Button("确定", id="repair-cancel-yes", color="danger"),
                ], style={"background": "red"}),
            ],
            id="repair-cancel-confirm-modal",
            is_open=False,
            centered=True,
            style={"border": "2px solid #1a4a7a", "borderRadius": "8px", "padding": "0", "boxShadow": "0 4px 8px rgba(0, 0, 0, 0.1)", "zIndex": "1000", "background": "red"}
        ),
        html.Div(
            html.Div(
                html.Div(
                    [
                        dbc.Button("新增报修", id="new-repair-btn", color="danger", className="mt-2", style={"height": "38px", "boxShadow": "1px 1px 3px #bbb", "cursor": "pointer"}),
                        html.Label("", style={"width": "50px"}),
                        dbc.Button("报修响应", id="repair-respond-button", color="success", className="btn-sm", style={"height": "38px", "boxShadow": "1px 1px 3px #bbb"}),
                        html.Label("", style={"width": "10px"}),
                        dbc.Button("维修完成", id="repair-complete-button", color="primary", className="btn-sm", style={"height": "38px", "boxShadow": "1px 1px 3px #bbb"}),
                        html.Label("", style={"width": "10px"}),
                        dbc.Button("工单", id="upload-proof-btn", color="info", className="btn-sm", style={"height": "38px", "boxShadow": "1px 1px 3px #bbb"}),
                        html.Label("", style={"width": "10px"}),
                        dbc.Button("取消维修", id="repair-order-cancel", color="danger", className="btn-sm", style={"height": "38px", "boxShadow": "1px 1px 3px #bbb", "display": "none"})
                    ],
                    style={"display": "flex", "position": "absolute", "left": "30px", "bottom": "30px", "zIndex": 999, "alignItems": "center"}
                )
            )
        ),
        html.Div(style={"clear": "both"})
    ], className="equipment-page repair-page")
    return layout






#扫二维码新增维修单
def create_repair_form_page2(id):
    # print(f"尝试查找设备ID: {id}")
    device = Product.query.get(id)
    # print(f"查找结果: {device}")
    if not device:
        return html.Div("设备不存在", style={"textAlign": "center", "marginTop": "50px"})

    return html.Div([
        dcc.ConfirmDialog(
                    id='call-repaire-dialog',
                    message=''
                ),
        html.H3("新增报修", style={"margin": "30px 0 20px 0", "textAlign": "center"}),
        dbc.Form([
            dbc.Row([
                dbc.Col([
                    dbc.Label("所属机构"),
                    dbc.Input(type="text", value=device.org_short_name_c, disabled=True, style={"backgroundColor": "#e9ecef"}),
                ], width=6),
                dbc.Col([
                    dbc.Label("所属科室"),
                    dbc.Input(type="text", value=device.department_name, disabled=True, style={"backgroundColor": "#e9ecef"}),
                ], width=6),
            ]),
            dbc.Row([
                dbc.Col([
                    dbc.Label("设备名称"),
                    dbc.Input(type="text", value=device.product_name, disabled=True, style={"backgroundColor": "#e9ecef"}),
                ], width=12),
            ]),
            dbc.Row([
                dbc.Col([
                    dbc.Label("故障现象", html_for="new-repair-fault2"),
                    dbc.Textarea(
                        id="new-repair-fault2",
                        placeholder="请描述故障现象",
                        style={"height": "20px"},
                        className="mb-3"
                    ),
                ], width=12),
            ]),
            dbc.Row([
                dbc.Col([
                    dbc.Label("叫修人姓名", html_for="new-repair-caller2"),
                    dbc.Input(
                        type="text",
                        id="new-repair-caller2",
                        placeholder="请输入叫修人姓名",
                        className="mb-3"
                    ),
                ], width=6),
            ]),
            # dbc.Alert(id="new-repair-alert2", color="danger", is_open=False, duration=4000),
            html.Div(
                [
                    dbc.Button("提交", id="new-repair-confirm2", color="primary", className="me-2"),
                    dbc.Button("放弃", id="new-repair-cancel2", color="secondary"),
                ],
                style={"textAlign": "center", "marginTop": "20px"}
            )
        ], style={"maxWidth": "600px", "margin": "0 auto"})
    ])
    